                                                                                                                                                     <template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="规则类型" prop="ruleType">
        <el-select v-model="queryParams.ruleType" placeholder="接单/退单" clearable>
          <el-option
            v-for="dict in dict.type.service_rule"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="提前预约时间" prop="ruleHour">-->
      <!--  <el-input-->
      <!--    v-model="queryParams.ruleHour"-->
      <!--    placeholder="请输入提前预约时间"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="退费比率" prop="ruleRefund">-->
      <!--  <el-input-->
      <!--    v-model="queryParams.ruleRefund"-->
      <!--    placeholder="请输入退费比率"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <el-form-item label="启用状态" prop="ruleUseFlag">
        <el-select v-model="queryParams.ruleUseFlag" placeholder="启用/禁用" clearable>
          <el-option
            v-for="dict in dict.type.service_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="0 正常 2 删除" prop="ruleDelFlag">-->
      <!--  <el-select v-model="queryParams.ruleDelFlag" placeholder="请选择0 正常 2 删除" clearable>-->
      <!--    <el-option-->
      <!--      v-for="dict in dict.type.service_yes_no"-->
      <!--      :key="dict.value"-->
      <!--      :label="dict.label"-->
      <!--      :value="dict.value"-->
      <!--    />-->
      <!--  </el-select>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="创建者" prop="ruleCreateBy">-->
      <!--  <el-input-->
      <!--    v-model="queryParams.ruleCreateBy"-->
      <!--    placeholder="请输入创建者"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="创建时间">-->
      <!--  <el-date-picker-->
      <!--    v-model="daterangeRuleCreateTime"-->
      <!--    style="width: 240px"-->
      <!--    value-format="yyyy-MM-dd"-->
      <!--    type="daterange"-->
      <!--    range-separator="-"-->
      <!--    start-placeholder="开始日期"-->
      <!--    end-placeholder="结束日期"-->
      <!--  ></el-date-picker>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="更新者" prop="ruleUpdateBy">-->
      <!--  <el-input-->
      <!--    v-model="queryParams.ruleUpdateBy"-->
      <!--    placeholder="请输入更新者"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="更新时间">-->
      <!--  <el-date-picker-->
      <!--    v-model="daterangeRuleUpdateTime"-->
      <!--    style="width: 240px"-->
      <!--    value-format="yyyy-MM-dd"-->
      <!--    type="daterange"-->
      <!--    range-separator="-"-->
      <!--    start-placeholder="开始日期"-->
      <!--    end-placeholder="结束日期"-->
      <!--  ></el-date-picker>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="备注" prop="ruleRemark">-->
      <!--  <el-input-->
      <!--    v-model="queryParams.ruleRemark"-->
      <!--    placeholder="请输入备注"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['service:rule:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['service:rule:edit']"
        >修改</el-button>
      </el-col>

      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--    type="danger"-->
      <!--    plain-->
      <!--    icon="el-icon-delete"-->
      <!--    size="mini"-->
      <!--    :disabled="multiple"-->
      <!--    @click="handleDelete"-->
      <!--    v-hasPermi="['service:rule:remove']"-->
      <!--  >删除</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--    type="warning"-->
      <!--    plain-->
      <!--    icon="el-icon-download"-->
      <!--    size="mini"-->
      <!--    @click="handleExport"-->
      <!--    v-hasPermi="['service:rule:export']"-->
      <!--  >导出</el-button>-->
      <!--</el-col>-->

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ruleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="ruleId" />
      <el-table-column label="规则类型" align="center" prop="ruleType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_rule" :value="scope.row.ruleType"/>
        </template>
      </el-table-column>
      <el-table-column label="需提前预约小时" align="center" prop="ruleHour" />
      <el-table-column label="订单状态" align="center" prop="ruleFlag" />
      <el-table-column label="退费比率" align="center" prop="ruleRefund" />
      <el-table-column label="启用状态" align="center" prop="ruleUseFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_yes_no" :value="scope.row.ruleUseFlag"/>
        </template>
      </el-table-column>
      <!--<el-table-column label="0 正常 2 删除" align="center" prop="ruleDelFlag">-->
      <!--  <template slot-scope="scope">-->
      <!--    <dict-tag :options="dict.type.service_yes_no" :value="scope.row.ruleDelFlag"/>-->
      <!--  </template>-->
      <!--</el-table-column>-->
      <el-table-column label="创建者" align="center" prop="ruleCreateBy" />
      <el-table-column label="创建时间" align="center" prop="ruleCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ruleCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="ruleUpdateBy" />
      <el-table-column label="更新时间" align="center" prop="ruleUpdateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ruleUpdateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="ruleRemark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['service:rule:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['service:rule:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改接单退单规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="规则类型" prop="ruleType">
          <el-select v-model="form.ruleType" placeholder="接单/退单">
            <el-option
              v-for="dict in dict.type.service_rule"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <p>
          以下填写的规则，只对接单规则有效
        </p>
        <el-form-item label="需提前预约小时数" prop="ruleHour">
          <el-input v-model="form.ruleHour" placeholder="请输入提前预约时间" />
        </el-form-item>
        <p>
          以下填写的规则，只对退单规则有效
        </p>
        <el-form-item label="订单所处状态" prop="ruleFlag">
          <el-select v-model="form.ruleFlag" placeholder="请选择所作用订单状态">
            <el-option
              v-for="its in orderFlags"
              :key="its.flagCode"
              :label="its.flagDesc"
              :value="its.flagCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="退费比率" prop="ruleRefund">
          <el-input v-model="form.ruleRefund" placeholder="请输入退费比率" />
        </el-form-item>
        <p>
          以下填写的规则，所有规则有效
        </p>
        <el-form-item label="启用状态" prop="ruleUseFlag">
          <el-select v-model="form.ruleUseFlag" placeholder="请选择0 启用 2禁用">
            <el-option
              v-for="dict in dict.type.service_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="0 正常 2 删除" prop="ruleDelFlag">-->
        <!--  <el-select v-model="form.ruleDelFlag" placeholder="请选择0 正常 2 删除">-->
        <!--    <el-option-->
        <!--      v-for="dict in dict.type.service_yes_no"-->
        <!--      :key="dict.value"-->
        <!--      :label="dict.label"-->
        <!--      :value="dict.value"-->
        <!--    ></el-option>-->
        <!--  </el-select>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="创建者" prop="ruleCreateBy">-->
        <!--  <el-input v-model="form.ruleCreateBy" placeholder="请输入创建者" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="创建时间" prop="ruleCreateTime">-->
        <!--  <el-date-picker clearable-->
        <!--    v-model="form.ruleCreateTime"-->
        <!--    type="date"-->
        <!--    value-format="yyyy-MM-dd"-->
        <!--    placeholder="请选择创建时间">-->
        <!--  </el-date-picker>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="更新者" prop="ruleUpdateBy">-->
        <!--  <el-input v-model="form.ruleUpdateBy" placeholder="请输入更新者" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="更新时间" prop="ruleUpdateTime">-->
        <!--  <el-date-picker clearable-->
        <!--    v-model="form.ruleUpdateTime"-->
        <!--    type="date"-->
        <!--    value-format="yyyy-MM-dd"-->
        <!--    placeholder="请选择更新时间">-->
        <!--  </el-date-picker>-->
        <!--</el-form-item>-->
        <el-form-item label="备注" prop="ruleRemark">
          <el-input v-model="form.ruleRemark" placeholder="备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRule, getRule, delRule, addRule, updateRule } from "@/api/service/rule";
import { listFlag, getFlag, delFlag, addFlag, updateFlag } from "@/api/service/flag";

export default {
  name: "Rule",
  dicts: ['service_rule', 'service_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 接单退单规则表格数据
      ruleList: [],
      // 订单状态数据
      orderFlags: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeRuleCreateTime: [],
      // 备注时间范围
      daterangeRuleUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ruleType: null,
        ruleHour: null,
        ruleFlag: null,
        ruleFlagId: null,
        ruleRefund: null,
        ruleUseFlag: null,
        ruleDelFlag: null,
        ruleCreateBy: null,
        ruleCreateTime: null,
        ruleUpdateBy: null,
        ruleUpdateTime: null,
        ruleRemark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询接单退单规则列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRuleCreateTime && '' != this.daterangeRuleCreateTime) {
        this.queryParams.params["beginRuleCreateTime"] = this.daterangeRuleCreateTime[0];
        this.queryParams.params["endRuleCreateTime"] = this.daterangeRuleCreateTime[1];
      }
      if (null != this.daterangeRuleUpdateTime && '' != this.daterangeRuleUpdateTime) {
        this.queryParams.params["beginRuleUpdateTime"] = this.daterangeRuleUpdateTime[0];
        this.queryParams.params["endRuleUpdateTime"] = this.daterangeRuleUpdateTime[1];
      }
      listRule(this.queryParams).then(response => {
        this.ruleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getFlags() {
      listFlag(this.queryParams).then(response => {
        this.orderFlags = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ruleId: null,
        ruleType: null,
        ruleHour: null,
        ruleFlagId: null,
        ruleFlag: null,
        ruleRefund: null,
        ruleUseFlag: null,
        ruleDelFlag: null,
        ruleCreateBy: null,
        ruleCreateTime: null,
        ruleUpdateBy: null,
        ruleUpdateTime: null,
        ruleRemark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRuleCreateTime = [];
      this.daterangeRuleUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ruleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getFlags();
      this.open = true;
      this.title = "添加接单退单规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ruleId = row.ruleId || this.ids
      getRule(ruleId).then(response => {
        this.form = response.data;
        this.getFlags();
        this.open = true;
        this.title = "修改接单退单规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ruleId != null) {
            updateRule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ruleIds = row.ruleId || this.ids;
      this.$modal.confirm('是否确认删除接单退单规则编号为"' + ruleIds + '"的数据项？').then(function() {
        return delRule(ruleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('service/rule/export', {
        ...this.queryParams
      }, `rule_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
